package com.xhu.servlet;

import com.xhu.bean.pojo.SealControl;
import com.xhu.service.SealControlService;
import com.xhu.service.impl.SealControlServiceImpl;
import com.xhu.utils.BaseServlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@WebServlet("/SealControlServlet/*")
public class SealControlServlet extends BaseServlet {

    SealControlService sealControlService = new SealControlServiceImpl();

    public Map addSeal(HttpServletRequest request){
        String title = request.getParameter("title");
        String type = request.getParameter("type");
        String state = request.getParameter("state");
        String date = request.getParameter("date");
        String controller = request.getParameter("controller");
        SealControl sealControl = new SealControl();
        sealControl.setState(state);
        sealControl.setTitle(title);
        sealControl.setType(type);
        sealControl.setDate(Date.valueOf(date));
        sealControl.setController(controller);


        Map<String,Object> map = new HashMap<>();

        if(sealControlService.addService(sealControl)){
            map.put("code", 0);
            map.put("msg", "添加成功");
        }else{
            map.put("code", -1);
            map.put("msg", "添加失败");
        }
        return map;
    }

    public Map deleteSeal(HttpServletRequest request){
        String  id = request.getParameter("id");
        SealControl sealControl = new SealControl();
        sealControl.setId(Integer.parseInt(id));
        Map<String,Object> map = new HashMap<>();
        if(sealControlService.deleteService(sealControl)){
            map.put("code", 0);
            map.put("msg", "删除成功");
        }else{
            map.put("code", -1);
            map.put("msg", "删除失败");
        }
        return map;
    }

    public Map updateSeal(HttpServletRequest request){
        String title = request.getParameter("title");
        String type = request.getParameter("type");
        String state = request.getParameter("state");
        String date = request.getParameter("date");
        String controller = request.getParameter("controller");
        SealControl sealControl = new SealControl();
        sealControl.setState(state);
        sealControl.setTitle(title);
        sealControl.setType(type);
        sealControl.setDate(Date.valueOf(date));
        sealControl.setController(controller);


        Map<String,Object> map = new HashMap<>();


        if (sealControlService.updateService(sealControl)){
            map.put("code", 0);
            map.put("msg", "更新成功");
        }else{
            map.put("code", -1);
            map.put("msg", "更新失败");
        }
        return map;
    }

    public Map getAllSeal(HttpServletRequest request){
        Map<String,Object> map = new HashMap<>();
        try{
            List<SealControl> list = sealControlService.getAllService();
            map.put("code", 0);
            map.put("msg", "获取成功");
            map.put("data",list);
        }catch (Exception e){
            System.out.println(e);
            map.put("code", -1);
            map.put("msg", "获取失败");
            map.put("data",null);
        }
        return map;
    }

    public Map getSome(HttpServletRequest request){

        Map<String,Object> map = new HashMap<>();

        try{
            List<SealControl> list = sealControlService.getSomeService(Date.valueOf(request.getParameter("date")));
            map.put("code", 0);
            map.put("msg", "获取成功");
            map.put("data",list);
        }catch (Exception e){
            System.out.println(e);
            map.put("code", -1);
            map.put("msg", "获取失败");
            map.put("data",null);
        }
        return map;
    }
}
